﻿Imports System.Reflection
Namespace UserInterface
    Public Class UIDetails
        ''' <summary>Used in dbiWSFObjectTree control</summary>
        Public Enum enumTreeLevel
            Initialize = 0
            Warehouse = 1
            LoadingDock = 2
            Shipment = 3
            OrderHeader = 4
            OrderDetail = 5
            All = 99
        End Enum
    End Class

    ''' <summary>Used in dbiWSFObjectDetail to layout the controls.</summary>
    Public Class DisplayControl
        Private l_LabelString As String
        Private l_ControlType As System.Windows.Forms.Control
        Private l_ControlReadOnly As Boolean
        Private l_ControlValue As Object
        Private l_ControlWidth As Long
        Private l_ControlHeight As Long
        Private l_ActualDataColumn As DataColumn



        Public Property LabelString() As String
            Get
                Return l_LabelString
            End Get
            Set(ByVal value As String)
                l_LabelString = value
            End Set
        End Property
        Public Property ControlType() As System.Windows.Forms.Control
            Get
                Return l_ControlType
            End Get
            Set(ByVal value As System.Windows.Forms.Control)
                l_ControlType = value
            End Set
        End Property

        Public Property ControlReadOnly() As Boolean
            Get
                Return l_ControlReadOnly
            End Get
            Set(ByVal value As Boolean)
                l_ControlReadOnly = value
            End Set
        End Property

        Public Property ControlValue() As Object
            Get
                Return l_ControlValue
            End Get
            Set(ByVal value As Object)
                l_ControlValue = value
            End Set
        End Property

        Public Property ControlWidth() As Long
            Get
                Return l_ControlWidth
            End Get
            Set(ByVal value As Long)
                l_ControlWidth = value
            End Set
        End Property

        Public Property ControlHeight() As Long
            Get
                Return l_ControlHeight
            End Get
            Set(ByVal value As Long)
                l_ControlHeight = value
            End Set
        End Property

        Public Property ActualDataColumn() As DataColumn
            Get
                Return l_ActualDataColumn
            End Get
            Set(ByVal value As DataColumn)
                l_ActualDataColumn = value
            End Set
        End Property
    End Class

    Public Class SharedMethods
        ''' <summary>Can be used to update an entire form to the database.</summary>
        '''<remarks>You need to tag the control in your form with a string which is the property name the
        ''' control is attached to.</remarks>
        Public Shared Sub UpdateControlsToWSFObject(ByRef ContainerObj As Object, ByRef WSFObjectToUpdate As Object, ByRef ParentWSFObject As dbiWSF.FrameworkObjects.objWSFObjects)
            Try
                If ContainerObj.Controls.Count > 0 Then
                    Dim curProperty As String
                    Dim PropInfo As PropertyInfo
                    For Each ctrl As System.Windows.Forms.Control In ContainerObj.Controls
                        If ctrl.Tag IsNot Nothing Then
                            If ctrl.Tag.GetType Is GetType(System.String) Then
                                Try
                                    curProperty = ParentWSFObject.WSFDatabinding.GetColumnMap(GetCMAP(WSFObjectToUpdate, ParentWSFObject), ctrl.Tag)
                                    Try
                                        PropInfo = WSFObjectToUpdate.GetType.GetProperty(curProperty)
                                        If Not PropInfo Is Nothing Then
                                            PropInfo.SetValue(WSFObjectToUpdate, GetValue(ctrl, PropInfo), Nothing)
                                            'System.Console.WriteLine(PropInfo.Name + " | " + curRow.Item(curColumn).ToString)
                                        End If
                                    Catch ex As Exception
                                        '<GenericError> Modify as needed
                                        Throw New Exception("There was an error mapping the data: " + ctrl.Tag, ex)
                                    End Try

                                Catch ex As Exception
                                    '<GenericError> Modify as needed
                                    Throw New Exception("There was an error mapping the data: " + ctrl.Tag, ex)
                                End Try
                            End If

                        End If
                    Next
                End If
            Catch ex As Exception

            End Try
        End Sub

        ''' <summary>Supporting Method: Used with UpdateControlsToWSFObject</summary>
        Private Shared Function GetCMAP(ByVal dbiWSFObject As Object, ByVal ParentWSFObject As dbiWSF.FrameworkObjects.objWSFObjects) As dbiWSF.Databinding.ColumnMapping
            Dim l_Cmap As dbiWSF.Databinding.ColumnMapping
            Select Case dbiWSFObject.GetType.ToString
                Case GetType(dbiWSF.FrameworkObjects.objWarehouse).ToString
                    l_Cmap = ParentWSFObject.WSFDatabinding.cmapWarehouses

                Case GetType(dbiWSF.FrameworkObjects.objLoadingDock).ToString
                    l_Cmap = ParentWSFObject.WSFDatabinding.cmapLoadingDocks

                Case GetType(dbiWSF.FrameworkObjects.objShipment).ToString
                    l_Cmap = ParentWSFObject.WSFDatabinding.cmapShipments

                Case GetType(dbiWSF.FrameworkObjects.objOrderHeader).ToString
                    l_Cmap = ParentWSFObject.WSFDatabinding.cmapOrderHeaders

                Case GetType(dbiWSF.FrameworkObjects.objOrderDetail).ToString
                    l_Cmap = ParentWSFObject.WSFDatabinding.cmapOrderDetails

                Case GetType(dbiWSF.FrameworkObjects.objCargoType).ToString
                    l_Cmap = ParentWSFObject.WSFDatabinding.cmapCargoTypes

                Case GetType(dbiWSF.FrameworkObjects.objLoadingDockException).ToString
                    l_Cmap = ParentWSFObject.WSFDatabinding.cmapLoadingDockExceptions

                Case GetType(dbiWSF.FrameworkObjects.objRecurrence).ToString
                    l_Cmap = ParentWSFObject.WSFDatabinding.cmapRecurrences

                Case Else
                    l_Cmap = Nothing
            End Select
            Return l_Cmap
        End Function

        ''' <summary>Supporting Method: Used with UpdateControlsToWSFObject</summary>
        Private Shared Function GetValue(ByRef CtrlToEvaluate As Object, ByVal PropToEval As PropertyInfo) As Object
            Dim objValue As Object
            Dim retValue As Object
            Select Case CtrlToEvaluate.GetType.ToString
                Case GetType(Windows.Forms.TextBox).ToString
                    objValue = CtrlToEvaluate.Text
                Case GetType(Windows.Forms.NumericUpDown).ToString
                    objValue = CtrlToEvaluate.Value
                Case GetType(Windows.Forms.MaskedTextBox).ToString
                    objValue = CtrlToEvaluate.Text
                Case GetType(dbiWSF.ControlLibrary.dbiDateTime).ToString
                    objValue = CtrlToEvaluate.DateTimeValue
                Case Else
                    objValue = Nothing
            End Select

            Select Case PropToEval.PropertyType.FullName
                Case GetType(System.Guid).ToString
                    retValue = New Guid(objValue.ToString)
                Case GetType(System.Int64).ToString
                    retValue = CLng(objValue)
                Case GetType(System.Int32).ToString
                    retValue = CInt(objValue)
                Case GetType(System.Double).ToString
                    retValue = CDbl(objValue)
                Case GetType(System.DateTime).ToString
                    retValue = CDate(objValue)
                Case Else
                    retValue = objValue.ToString
            End Select
            Return retValue

        End Function
    End Class

End Namespace
